package com.sjz.myfrist;

import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.PlayerView;

/***
 *   ExoPlayer是由Google开发的媒体播放器库，支持播放本地和网络视频。
 *   implementation 'com.google.android.exoplayer:exoplayer-core:2.15.1'
 *   implementation 'com.google.android.exoplayer:exoplayer-ui:2.15.1'
 */
public class ExoplayActivity extends AppCompatActivity {

    private SimpleExoPlayer mPlayer;
    private PlayerView mPlayerView;
    private Button mBtnZoom;
    private boolean isFullscreen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exoplay);
        mPlayerView = findViewById(R.id.player_view);
        mBtnZoom = findViewById(R.id.btn_zoom);

//         创建媒体播放器
        mPlayer = new SimpleExoPlayer.Builder(this).build();
        mPlayerView.setPlayer(mPlayer);

//        准备视频源
        String videoUrl = "https://v2.kwaicdn.com/upic/2023/04/14/13/BMjAyMzA0MTQxMzA3MDhfNDgwOTQ5MTg2XzEwMDU1NzkwNjgxMV8yXzM=_b_B4856a8b07ea4dbc4ad503f04681b5ec8.mp4?pkey=AAW-JkY6q-BoTEXl2FArO1ReMGt965IaFcPl0NGX7cYTVhLtgZVdld15RtYUAWwkkEmBjjKBPE2yDFb0kigIji2xGTYcxXU-rCTrXNQiN4N4_RpUja5SPyx99Eh46Cixcag&tag=1-1684724589-unknown-0-0eoiahcqin-0a0841bce7d498e5&clientCacheKey=3xt93xmd7emzv6k_b.mp4";
        String videoUrl2 = "https://v2.kwaicdn.com/upic/2023/05/20/16/BMjAyMzA1MjAxNjAyMjhfMTM2MDg5NzAyNl8xMDM1MjIwNDgzMjFfMl8z_b_B0d97abd06757be9906cd83e0571dcd7d.mp4?pkey=AAX4DgH_Q4KBjVVQpeITMyNxi34_0KOD7Dp80qxpwuNV4BfONaasSnAkBrFPEdGKbfj0m9Jd_VGG-isTQxiQGOlUQ6e2OKuPO2f72pFUVdxdEJkqCVpijL4zsJu3mMtoBfc&tag=1-1684724589-unknown-0-hilddj6arr-772b90a0f590e795&clientCacheKey=3xhd9ng5fbii652_b.mp4";
        MediaItem mediaItem = MediaItem.fromUri(videoUrl);
        MediaItem mediaItem2 = MediaItem.fromUri(videoUrl2);
        mPlayer.setMediaItem(mediaItem);
        mPlayer.prepare();


//        按钮放大缩小
        mBtnZoom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toggleFullscreen();
            }
        });


//        双击视频放大缩小
        mPlayerView.setOnTouchListener(new View.OnTouchListener() {
            private GestureDetector gestureDetector = new GestureDetector(ExoplayActivity.this,
                    new GestureDetector.SimpleOnGestureListener() {
                        @Override
                        public boolean onDoubleTap(MotionEvent e) {
                            toggleFullscreen();
                            return super.onDoubleTap(e);
                        }
                    });

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                gestureDetector.onTouchEvent(motionEvent);
                return true;
            }
        });


    }



    //    视频放大缩小方法
    private void toggleFullscreen() {
        if (isFullscreen) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            mPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
            getSupportActionBar().show();
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            mPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
            getSupportActionBar().hide();
        }
        isFullscreen = !isFullscreen;
    }


    //    以下是生命周期
    @Override
    protected void onStart() {
        super.onStart();
        mPlayerView.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPlayerView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPlayer.release();
    }


    //   注意：在使用ExoP注意：在使用ExoPlayer时，需要在Activity中保持屏幕常亮以避免视频播放过程中屏幕自动关闭。layer时，需要在Activity中保持屏幕常亮以避免视频播放过程中屏幕自动关闭。
    @Override
    protected void onResume() {
        super.onResume();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

}